package com.fenghongzhang.day019;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Main2Activity extends AppCompatActivity {
    private ListView musiclist;
    private Button pause;
    private Button pre;
    private Button next;
    private Button restart;
    private Button model;
    private List<Music> list;
    private SeekBar seekbar;

    private MusicService.MyBinder binder;
    private MusicService musicService;

    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        seekbar = (SeekBar) findViewById(R.id.seekbar);
        musiclist = (ListView) findViewById(R.id.musiclist);
        pause = (Button) findViewById(R.id.pause);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);
        restart = (Button) findViewById(R.id.restart);
        model = (Button) findViewById(R.id.model);

        list = MusicUtil.getMusics(this);


        //start
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        //bind
        //调用服务中的方法而生的.
        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //代理对象
                binder = (MusicService.MyBinder) service;
                musicService =  binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);


//        seekbar.setMax(?);

//        seekbar.setProgress(?);


        MusicAdapter musicAdapter = new MusicAdapter(list, this);
        musiclist.setAdapter(musicAdapter);


        //拖动的功能
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    //传值
                    musicService.seetTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        musiclist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                binder.callPlay(position);
//                play()?
                musicService.play(position);

                //开始的时候
                if(timer != null){
                    timer.cancel();
                }
                 timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //ui
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                seekbar.setMax(musicService.getMax());
                                seekbar.setProgress(musicService.getPosition());
                            }
                        });

                    }
                },0,1000);
            }
        });
    }

    public void pause(View view) {
//        binder.callPause();
        musicService.pause();
    }

    public void restart(View view) {
        musicService.restart();
    }


    public void next(View view) {
        musicService.next();
    }
}
